//
//  UserCenter.Sign.Mobile.swift
//  Center
//
//  Created by 蒋晨成 on 2022/1/5.
//

import Center_RPC
import Foundation
import LogMan

public extension UserCenter.Sign {
    enum Mobile {}
}

public extension UserCenter.Sign.Mobile {
    private(set) static var isLogin: Bool = false

    static func loginSuccess() {
        isLogin = true
    }

    static func logoutSuccess() {
        isLogin = false
    }
}

public extension UserCenter.Sign.Mobile {
    static func auth(mobile: String, code: String, complete: @escaping (Result<Void, Error>) -> Void) {
        RPCCenter.azir.mobileAuth(mobile: mobile, code: code) { result in
            switch result {
            case .success(let response):
                let userInfo = UserInfo(response: response.userInfo)
                UserCenter.update(sessionID: response.sessionID)
                UserCenter.update(userInfo: userInfo)
                UserCenter.logger.info("mobileAuth success: \(String(describing: userInfo))")
                complete(.success(()))
            case .failure(let error):
                UserCenter.logger.error("mobileAuth error: \(error)")
                complete(.failure(error))
            }
        }
    }

    static func sendSMS(mobile: String, complete: @escaping (Result<Void, Error>) -> Void) {
        RPCCenter.azir.sendSMS(mobile: mobile) { result in
            switch result {
            case .success:
                complete(.success(()))
            case .failure(let error):
                UserCenter.logger.error("sendSMS error: \(error)")
                complete(.failure(error))
            }
        }
    }
}
